<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <!-- <a-button type="primary" @click="handleForm({ op: 'add' })" v-auth="'sys:empUser:edit'">
          <Icon icon="del-icon:add-1" /> {{ t('删除') }}
        </a-button> -->
        <Popconfirm :title="t('是否确认删除选中的数据吗？')" @confirm="handleDeleteSelected()">
          <a-button danger type="default" class="ml-4">
            <Icon icon="i-ant-design:delete-outlined" color="error" /> {{ t('删除') }} ({{
              selectedRowKeysRef.length
            }})
          </a-button>
        </Popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ userCode: record.userCode, op: 'edit' })" :title="record.loginCode">
          {{ record.loginCode }}
        </a>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup name="ViewsSysEmpUserList">
  import { onMounted, watch, ref, unref } from 'vue';
  import { Popconfirm } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { empUserDelete, empUserListData, empUserList } from '/@/api/sys/empUser';
  import { resetpwd, empUserDisable, empUserEnable } from '/@/api/sys/empUser';
  import { FormProps } from '/@/components/Form';
  import { useLocaleStore } from '/@/store/modules/locale';
  import { LOCALE } from '/@/settings/localeSetting';
  import { boxFreeListData, deleteById } from '/@/api/wms/btdm/boxfree';

  const { t } = useI18n('sys.empUser');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const getTitle = {
    icon: meta.icon || 'simple-line-icons:user',
    value: meta.title || t('箱子空余管理'),
  };
  const ctrlPermi = ref('');
  const postList = ref<Recordable>([]);
  const roleList = ref<Recordable>([]);
  const localeStore = useLocaleStore();

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: localeStore.getLocale == LOCALE.ZH_CN ? 100 : 100,
    showAdvancedButton: true,
    schemas: [
      {
        label: t('箱码'),
        field: 'boxCode',
        component: 'Input',
      },
      {
        label: t('导出次数'),
        field: 'exportCount',
        component: 'Input',
      },
      {
        label: t('日期选择'),
        field: 'testDate',
        component: 'RangePicker',
        componentProps: {
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_box_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['testDate', ['testDate_gte', 'testDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('箱码'),
      dataIndex: 'boxCode',
      key: 'a.box_code',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('箱子状态'),
      dataIndex: 'status',
      key: 'a.status',
      dictType: 'btdm_box_status',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('出库单号'),
      dataIndex: 'ccode',
      key: 'a.ccode',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      key: 'a.update_date',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('导出次数'),
      dataIndex: 'exportCount',
      key: 'a.export_count',
      sorter: true,
      width: 80,
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
    },
  ];
  const selectedRowKeysRef = ref<string[]>([]);

  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: boxFreeListData,
    beforeFetch: (params) => {
      return params;
    },
    immediate: false,
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    showSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
    defaultRowSelection: {
      onChange: (selectedRowKeys: string[], _selectedRows: Recordable[]) => {
        selectedRowKeysRef.value = selectedRowKeys;
      },
    },
  });
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/btdm/boxfree/exportData',
    });
  }
  async function handleDeleteSelected() {
    if (getSelectRows().length) {
      var selStr = ref('');
      getSelectRows().forEach((item) => {
        selStr.value += item.id + ',';
      });
      const res = await deleteById({ selStr: selStr.value });
      selectedRowKeysRef.value = [];
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择数据！！');
    }
  }
  function handleSuccess(_record: Recordable) {
    reload();
  }
</script>
